package cn.school.controller.sys;

import cn.school.common.annotation.SysLog;
import cn.school.common.api.CommonPage;
import cn.school.common.api.CommonResult;
import cn.school.common.enums.BusinessType;
import cn.school.sys.entity.TermTime;
import cn.school.sys.service.TermTimeService;
import com.baomidou.mybatisplus.core.metadata.IPage;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.Map;

@RestController
@Api(tags = "Term-Time => 学年学期管理")
@RequestMapping("/sys/termtime")
public class TermTimeController {
    @Autowired
    private TermTimeService termTimeService;


    /**
     * @param params 参数内容
     *               pageNum
     *               pageSize
     * @return cn.harry.common.api.CommonPage
     */
    @ApiOperation("list => 获取学年学期分页列表")
//    @PreAuthorize("@ss.hasPermi('system:termtime:list')")
    @GetMapping(value = "/list")
    public CommonResult<CommonPage<TermTime>> list(@RequestParam Map<String, Object> params) {
        IPage<TermTime> page = termTimeService.getPage(params);
        return CommonResult.success(CommonPage.restPage(page));
    }

    @ApiOperation("info/{id} => 学年学期信息")
//    @PreAuthorize("@ss.hasPermi('system:termtime:query')")
    @GetMapping("/info/{id}")
    public CommonResult<TermTime> info(@PathVariable("id") Long id) {
        TermTime termTime = this.termTimeService.getTermTimeById(id);
        return CommonResult.success(termTime);
    }

    @ApiOperation("update/{id} => 修改指定学年学期")
    @PreAuthorize("@ss.hasPermi('system:termtime:edit')")
    @SysLog(title = "修改指定学年学期", businessType = BusinessType.UPDATE)
    @PutMapping(value = "/update/{id}")
    public CommonResult<Integer> update(@PathVariable("id") Long id, @RequestBody TermTime termTime) {
        int count = this.termTimeService.update(id, termTime);
        if (count > 0) {
            return CommonResult.success(count);
        }
        return CommonResult.failed();
    }

    @ApiOperation("create => 新建学年学期")
//    @PreAuthorize("@ss.hasPermi('system:termtime:add')")
    @SysLog(title = "新建学年学期", businessType = BusinessType.INSERT)
    @PostMapping(value = "/create")
    public CommonResult<Integer> create(@RequestBody TermTime termTime) {
        int count = this.termTimeService.create(termTime);
        if (count > 0) {
            return CommonResult.success(count);
        }
        return CommonResult.failed();
    }

    @ApiOperation("deleteByIds/{ids} => 删除指定学年学期")
    @PreAuthorize("@ss.hasPermi('system:termtime:remove')")
    @SysLog(title = "删除指定学年学期", businessType = BusinessType.DELETE)
    @DeleteMapping(value = "/deleteByIds/{ids}")
    public CommonResult<Integer> deleteByIds(@PathVariable Long[] ids) {
        int count = this.termTimeService.deleteByIds(ids);
        if (count > 0) {
            return CommonResult.success(count);
        }
        return CommonResult.failed();
    }
}
